package com.experience.we.lambda;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.BiPredicate;
import java.util.function.Consumer;
import java.util.stream.Collectors;

public class Lambda1 {
	
	/*private static List<Apple> filter(List<Apple> apple, Predicate<Apple> pre){
		List<Apple> result = new ArrayList<>();
		apple.forEach(s->{
			if(pre.test(s)) {
				result.add(s);
			}
		});
		return result;
	}*/
	
	private static List<Apple> filter2If(List<Apple> apple, BiPredicate<String, Integer> biPre){
		List<Apple> result = new ArrayList<>();
		apple.forEach(s->{
			if(biPre.test(s.getColor(), s.getWeight())) {
				result.add(s);
			}
		});
		return result;
	}
	
	private static void simpleTestConsumer(List<Apple> apple, Consumer<Apple> consumer) {
		apple.forEach(s->{
			consumer.accept(s);
		});
	}
	
	
	
	public static void main(String[] args) {
		List<Apple> apl = Arrays.asList(new Apple("green",120),new Apple("green", 150),new Apple("yello", 150),new Apple("green", 150));
		
		//List<Apple> result = filter(apl,(s)->s.getColor().equals("green"));
		List<Apple> result2 = filter2If(apl, (s,w)->s.equals("green")&&w.equals(120));
		simpleTestConsumer(apl, a-> System.out.println(a));
		System.out.println("=====================================");
		System.out.println(result2);


		//分组
		Map<String, List<Apple>> collect = apl.stream().collect(Collectors.groupingBy(Apple::getColor));
		System.out.println(collect);


	}
}
